/** @format */
/**
 * @desc: 工具类--表格导出
 * @author: ptt
 * @date  2023/12/12
 */
export default {
    /**
     * @desc: 导出excel
     * @param: 字符串格式的table每一行【String】,
              文件名【String】,
              导出按钮中a标签对应的id名【String】
     * @returns: */
    exportExcel: function (trStr, fileName, id) {
        //Worksheet名
        let uri = 'data:application/vnd.ms-excel;base64,';

        //下载的表格模板数据
        let template = `<html xmlns:o="urn:schemas-microsoft-com:office:office"
      xmlns:x="urn:schemas-microsoft-com:office:excel"
      xmlns="http://www.w3.org/TR/REC-html40">
      <head><meta charset="UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
        <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
        </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
        </head><body><table>${trStr}</table></body></html>`;

        //下载模板
        function base64(s) {
            return window.btoa(unescape(encodeURIComponent(s)));
        }

        document.getElementById(id).href = uri + base64(template);
        document.getElementById(id).download = fileName;
        document.getElementById(id).click();
    }
};
